package com.taptech.doufu.ui.view.userdetail

import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.TextView
import com.google.gson.Gson
import com.taptech.doufu.R
import com.taptech.doufu.model.novel.bean.ImageBean
import com.taptech.doufu.model.user.JournalBean
import com.taptech.doufu.model.user.ListItem
import com.taptech.doufu.ui.activity.MainHomeActivity
import com.taptech.doufu.ui.activity.weex.SimpleWeexActivity
import com.taptech.doufu.util.*
import kotlinx.android.synthetic.main.view_user_detail_journal.view.*

class UserDetailJournalView(context: Context):LinearLayout(context) {


    init {
        initView()
    }

    private fun initView() {
        val view = LayoutInflater.from(context).inflate(R.layout.view_user_detail_journal,null)
        addView(view)
    }


    fun setContentNullData() {
        tvContentNull.visibility = View.VISIBLE
        layGoWith.visibility = View.VISIBLE
        layMore.visibility = View.GONE
        layGoWith.setOnClickListener {
            val intent = Intent()
            intent.setClass(context, MainHomeActivity::class.java)
            intent.putExtra("index", 2)
            context.startActivity(intent)
        }
    }

    fun setData(uid:Int,data: ListItem) {
        try {
            val info = GsonUtil.parseJsonWithGson(Gson().toJson(data.data), JournalBean::class.java)
            setOnClickListener {
                SimpleWeexActivity.startActivity(context,"vue/find/TopicDetail/TFTopicDetail.js?id=${info.id}")
            }
            layMore.setOnClickListener {
                val map = HashMap<String, Any>()
                map["uid"] = uid
                SimpleWeexActivity.startActivity(context,"/vue/find/TFUserPostList.js",map)
            }
            tvCount.text = "(${data.count}个)"
            if (info.tags.size > 0 && !TextUtils.isEmpty(info.tags[0].name)) {
                tvUpdateTime.visibility = View.VISIBLE
                tvUpdateTime.text = DiaobaoUtil.seconds2TimeString(TimeDateFunction.getTimeToLong(info.created_at))
                layTag.visibility = View.VISIBLE
                layTag.removeAllViews()
                val textParams = LayoutParams(LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT)
                textParams.bottomMargin = ScreenUtil.dip2px(context,4f)
                for (tag in info.tags) {
                    val tvTag = TextView(context)
                    tvTag.text = "#${tag.name}#"
                    tvTag.textSize = 14f
                    tvTag.setTextColor(Color.parseColor("#6EB5FE"))
                    tvTag.layoutParams = textParams
                    tvTag.setOnClickListener {
                        val map = HashMap<String, Any>()
                        map["id"] = tag.id
                        map["name"] = tag.name
                        SimpleWeexActivity.startActivity(context,"vue/find/TFFindTopicCenter.js",map)
                    }
                    layTag.addView(tvTag)
                }
            }else{
                layTag.visibility = View.GONE
                tvUpdateTime.visibility = View.GONE
            }
            if (TextUtils.isEmpty(info.content)) {
                tvContent.visibility = View.GONE
            }else{
                tvContent.visibility = View.VISIBLE
                tvContent.text = info.content
            }
            setImages(info.images)

        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    fun setImages(images: MutableList<ImageBean>) {
        if (images.size > 0) {
            layPic.visibility = View.VISIBLE
            val width = (ScreenUtil.getScreenWidth(context)-ScreenUtil.dip2px(context,76f))/3
            ivPic_1.layoutParams = LayoutParams(width,width)
            val params = LayoutParams(width,width)
            params.leftMargin = ScreenUtil.dip2px(context,12f)
            ivPic_2.layoutParams = params
            ivPic_3.layoutParams = params
            ivPic_2.visibility = View.GONE
            ivPic_3.visibility = View.GONE
            GlideUtil.displayRoundImage(ivPic_1,images[0].url,3)
            if(images.size > 1){
                ivPic_2.visibility = View.VISIBLE
                GlideUtil.displayRoundImage(ivPic_2,images[1].url,3)
            }
            if (images.size > 2) {
                ivPic_3.visibility = View.VISIBLE
                GlideUtil.displayRoundImage(ivPic_3,images[2].url,3)
            }
            if (images.size > 3) {
                layPicCount.visibility = View.VISIBLE
                tvPicCount.text = "共${images.size}张"
            }else{
                layPicCount.visibility = View.GONE
            }
        }else{
            layPic.visibility = View.GONE
        }
    }
}
